package com.tianyi.wz.walkinggame.biz;

import android.app.Activity;
import android.util.Log;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.tianyi.wz.walkinggame.Constant;

public class CsjRewardVideoBiz implements IRewardVideoBiz {

    private CsjRewardVideoBiz(Activity activity,String userId,String codeId){
        mActivity = activity;
        mCodeId = codeId;
        mUserId = userId;
        init();
    }

    private TTAdManager ttAdManager;
    private TTAdNative mTTAdNative;

    private Activity mActivity;
    private String mCodeId;
    private String mUserId;

    public static CsjRewardVideoBiz getInstance(Activity activity,String userId,String codeId){
        CsjRewardVideoBiz biz = new CsjRewardVideoBiz(activity,userId,codeId);
        return biz;
    }

    private void init(){
        ttAdManager = TTAdSdk.getAdManager();
        ttAdManager.requestPermissionIfNecessary(mActivity);
        mTTAdNative = ttAdManager.createAdNative(mActivity.getApplicationContext());
    }

    private boolean mHasShowDownloadActive = false;

    public void showAd(){
//step4:创建广告请求参数AdSlot,具体参数含义参考文档
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(mCodeId)
                .setSupportDeepLink(true)
                .setImageAcceptedSize(1080, 1920)
                //.setRewardName("步步添金_激励视频_10_28_16:38")
                .setUserID(mUserId)//用户id,必传参数
                //.setRewardName("首页翻倍") //奖励的名称
                //.setRewardAmount(1)  //奖励的数量
                .setMediaExtra("media_extra") //附加参数，可选
                .setOrientation(TTAdConstant.VERTICAL) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                .build();
        Log.d(CsjRewardVideoBiz.class.getName(),
                "code_id - " + mCodeId + " \n " +
                        "user_id - " + mUserId + " \n " +
                        "app_id - " + Constant.SETTING.TT_APPID);
        //step5:请求广告
        mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int code, String message) {
                Log.d(CsjRewardVideoBiz.class.getName(),message);
                //ToastCommon.showToast(mActivity, message);
            }

            //视频广告加载后，视频资源缓存到本地的回调，在此回调后，播放本地视频，流畅不阻塞。
            @Override
            public void onRewardVideoCached() {
                Log.d(CsjRewardVideoBiz.class.getName(),"rewardVideoAd video cached");
                //ToastCommon.showToast(mActivity,  "rewardVideoAd video cached");
            }

            //视频广告的素材加载完毕，比如视频url等，在此回调后，可以播放在线视频，网络不好可能出现加载缓冲，影响体验。
            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
                //ToastCommon.showToast(mActivity, "rewardVideoAd loaded");
                Log.d(CsjRewardVideoBiz.class.getName(),"rewardVideoAd loaded");
                ad.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {

                    @Override
                    public void onAdShow() {
                        //ToastCommon.showToast(mActivity,  "rewardVideoAd show");
                        Log.d(CsjRewardVideoBiz.class.getName(),"rewardVideoAd show");
                        new LogBiz(mActivity).saveAd(mUserId,mCodeId,1);
                    }

                    @Override
                    public void onAdVideoBarClick() {
                        //ToastCommon.showToast(mActivity,  "rewardVideoAd bar click");
                        Log.d(CsjRewardVideoBiz.class.getName(),"rewardVideoAd bar click");
                    }

                    @Override
                    public void onAdClose() {
                        //ToastCommon.showToast(mActivity,  "rewardVideoAd close");
                        Log.d(CsjRewardVideoBiz.class.getName(),"rewardVideoAd close");
                    }

                    //视频播放完成回调
                    @Override
                    public void onVideoComplete() {
                        //ToastCommon.showToast(mActivity,  "rewardVideoAd complete");
                        Log.d(CsjRewardVideoBiz.class.getName(),"rewardVideoAd complete");
                    }

                    @Override
                    public void onVideoError() {
                        //ToastCommon.showToast(mActivity,  "rewardVideoAd error");
                        Log.d(CsjRewardVideoBiz.class.getName(),"rewardVideoAd error");
                    }

                    //视频播放完成后，奖励验证回调，rewardVerify：是否有效，rewardAmount：奖励梳理，rewardName：奖励名称
                    @Override
                    public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName) {
                        //ToastCommon.showToast(mActivity,  "verify:" + rewardVerify + " amount:" + rewardAmount + " name:" + rewardName);
                        Log.d(CsjRewardVideoBiz.class.getName(),"verify:" + rewardVerify + " amount:" + rewardAmount + " name:" + rewardName);
                    }

                    @Override
                    public void onSkippedVideo() {
                        //ToastCommon.showToast(mActivity, "rewardVideoAd has onSkippedVideo");
                        Log.d(CsjRewardVideoBiz.class.getName(),"rewardVideoAd has onSkippedVideo");
                    }
                });
                ad.setDownloadListener(new TTAppDownloadListener() {
                    @Override
                    public void onIdle() {
                        mHasShowDownloadActive = false;
                    }

                    @Override
                    public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                        if (!mHasShowDownloadActive) {
                            mHasShowDownloadActive = true;
                            //ToastCommon.showToast(mActivity, "下载中，点击下载区域暂停");
                            Log.d(CsjRewardVideoBiz.class.getName(),"下载中，点击下载区域暂停");
                        }
                    }

                    @Override
                    public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                        //ToastCommon.showToast(mActivity, "下载暂停，点击下载区域继续");
                        Log.d(CsjRewardVideoBiz.class.getName(),"下载暂停，点击下载区域继续");
                    }

                    @Override
                    public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                        //ToastCommon.showToast(mActivity, "下载失败，点击下载区域重新下载");
                        Log.d(CsjRewardVideoBiz.class.getName(),"下载失败，点击下载区域重新下载");
                    }

                    @Override
                    public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                        //ToastCommon.showToast(mActivity, "下载完成，点击下载区域重新下载");
                        Log.d(CsjRewardVideoBiz.class.getName(),"下载完成，点击下载区域重新下载");
                    }

                    @Override
                    public void onInstalled(String fileName, String appName) {
                        //ToastCommon.showToast(mActivity, "安装完成，点击下载区域打开");
                        Log.d(CsjRewardVideoBiz.class.getName(),"安装完成，点击下载区域打开");
                    }

                });
                ad.showRewardVideoAd(mActivity,TTAdConstant.RitScenes.CUSTOMIZE_SCENES,"scenes_test");
            }
        });
    }

}
